package com.sh.data.engine.domain.normalization.modeldesign.model.model.enums;

import com.sh.data.engine.domain.base.model.enums.ThemeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.directory.api.util.Strings;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : changkong
 * @date : 2022/1/17
 */
public enum ModelTypeEnum {
    ODS("ods", "ods"),
    DWD("dwd", "public,cdm"),
    DWS("dws", "public"),
    DIM("dim", "public,cdm"),
    ADS("ads", "apply"),
    TMP("tmp", "other");

    private final String name;
    private final String type;

    private ModelTypeEnum(String name, String type) {
        this.name = name;
        this.type = type;
    }

    /**
     * 根据模型类型获取名称前缀
     *
     * @param type
     * @return
     */
    public static List<String> getNameByType(String type) {
        List<String> res = new ArrayList<>();
        for (ModelTypeEnum m : values()) {
            // if (m.getType().equals(type)) {
            if (Arrays.asList(m.getType().split(",")).contains(type)) {
                res.add(m.getName());
            }
        }
        if (CollectionUtils.isEmpty(res)) {
            throw new IllegalArgumentException("非法参数");
        }
        return res;
    }

    public static List<String> getNameByThemeEnum(ThemeEnum themeEnum) {
        final ModelTypeEnum modelTypeEnum = getModelByName(themeEnum.getCode());

        if (modelTypeEnum == null) {
            return new ArrayList<>();
        }

        return getNameByType(modelTypeEnum.getType());
    }

    public static Boolean validate(String name) {
        return Arrays.stream(values()).anyMatch(i -> i.name.equals(name));
    }

    public static Boolean validate(String name, String type) {
        return Arrays.stream(values()).anyMatch(i -> i.name.equals(name) && i.type.equals(type));
    }

    public static ModelTypeEnum getModelByTableName(String tableName) {

        for (ModelTypeEnum modelTypeEnum : ModelTypeEnum.values()) {
            String name = modelTypeEnum.name;
            name = name + "_";
            if (Strings.toUpperCase(tableName).startsWith(Strings.toUpperCase(name))) {
                return modelTypeEnum;
            }
        }
        return null;
    }

    public static ModelTypeEnum getModelByName(String name) {
        for (ModelTypeEnum value : ModelTypeEnum.values()) {
            if (StringUtils.equals(value.name, name)) {
                return value;
            }
        }
        return null;
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }
}
